var Flasher = Class.create({
    texts : {},
    intervalID : 0,
    current : '',
    show : true,
    start : function(text)
    {
        if(this.intervalID==0) {
            this.intervalID = window.setInterval(this.flash.bind(this), 300);
        }
        this.show = true;
        this.texts[text] = true;
        this.current = text;
        return text;
    },
    
    setText : function(text,newText)
    {
        delete this.texts[text];
        this.texts[newText] = true;
        current = newText;
    },
    
    stop : function(text)
    {
        if(!text) stopAll();
        delete this.texts[text];
        if(Object.keys(this.texts).length == 0) {
            window.clearInterval(this.intervalID);
            this.intervalID = 0;
        }
    },
    stopAll : function()
    {
        this.texts = {};
        window.clearInterval(this.intervalID);
        this.intervalID = 0;
    },
    flash : function()
    {
        this.show = !this.show;
        if(this.show)
        {
            var shown = false;
            for(var text in this.texts)
            {
                if(shown)
                {
                    this.current = text;
                    chrome.browserAction.setBadgeText({text : this.current});
                    return;
                }
                if(text == this.current) shown = true;
            }
            
            for(var text in this.texts)
            {
                this.current = text;
                chrome.browserAction.setBadgeText({text : this.current});
                return;
            }
            
        } else chrome.browserAction.setBadgeText({text : ''});
    }
});
